#include "onlylyric-impl.h"
#include <nsEmbedString.h>
//#include <nsIObserverService.h>
//#include <nsComponentManagerUtils.h>
#include <string>
#include <iostream>
#include <nsIScriptableUConv.h>
#include "CharsetConvert.h"
#include "PlayerManager.h"

using namespace std;

NS_IMPL_ISUPPORTS2(COnlyLyric, IOnlyLyric, nsIObserver)

COnlyLyric::COnlyLyric()
{
	/* member initializers and constructor code */
}

COnlyLyric::~COnlyLyric()
{
	/* destructor code */
}

/* AString getString (); */
NS_IMETHODIMP COnlyLyric::GetString(nsAString & _retval)
{
	static std::string lastSongName = "";
	static std::string lastSearchName = "";


	if(!CPlayerManager::GetIns().IsPlayerOpen())
	{
		_retval.Assign(NS_LITERAL_STRING("Can't find the player!"));
		return NS_OK;
	}

	std::string songName = CPlayerManager::GetIns().GetMediaTitle();
    //cout << __FUNCTION__ << "song name = " << songName << endl;

	if (lastSongName == songName)
	{
		int time = CPlayerManager::GetIns().GetCurrentTime();
        cout << "time = " << time << endl;
		if (time == -1)
		{
			_retval.Assign(NS_LITERAL_STRING("Oop! What's wrong?"));
			return NS_OK;
		}

		std::string str = m_lrc.GetLrcString(time);
		_retval.Assign(CCharsetConvert::GetIns().ConvertToUnicode(nsCString(str.c_str())));
		return NS_OK;
	}

	std::string lrcFile = m_map.GetLrcFile(songName);
    //cout << "lrc file = " << lrcFile << endl;
	if (lrcFile.empty())
	{
		if (songName == lastSearchName)
		{
			_retval.Assign(NS_LITERAL_STRING("Can't find the lrc file."));
			//_retval.Assign(nsDependentString(songName.c_str()));
			return NS_OK;
		}

		lrcFile = m_searchLrc.GetLrcFile(songName);
		if (lrcFile.empty())
		{
			_retval.Assign(NS_LITERAL_STRING("Can't find the lrc file."));
			return NS_OK;
		}
		m_map.SetLrcFile(lrcFile);
	}

	m_lrc.SetLrcFile(lrcFile);

	int time = CPlayerManager::GetIns().GetCurrentTime();
    cout << "time = " << time << endl;
	if (time == -1)
	{
		_retval.Assign(NS_LITERAL_STRING("Oop! What's wrong?"));
		return NS_OK;
	}
	std::string str = m_lrc.GetLrcString(time);
	_retval.Assign(CCharsetConvert::GetIns().ConvertToUnicode(nsCString(str.c_str())));
	lastSongName = songName;
	return NS_OK;
}

NS_IMETHODIMP COnlyLyric::SetLrcFile(const nsAString & file)
{
	std::string str = CCharsetConvert::GetIns().ConvertToLocal(file).get();
    cout << "lrc file = " << str << endl;
	m_map.SetLrcFile(str);
	m_lrc.SetLrcFile(str);
	return NS_OK;
}

/* void SetPlayer (in ACString player); */
NS_IMETHODIMP COnlyLyric::SetPlayer(const char *player)
{
	CPlayerManager::GetIns().SetPlayer(player);
	return NS_OK;
}

/* void SetLrcDir (in AString dir); */
NS_IMETHODIMP COnlyLyric::SetLrcDir(const nsAString & dir)
{
	m_searchLrc.SetDir(CCharsetConvert::GetIns().ConvertToLocal(dir).get());
	return NS_OK;
}

/* void SetLrcEncode (in string encode); */
NS_IMETHODIMP COnlyLyric::SetLrcEncode(const char *encode)
{
	CCharsetConvert::GetIns().SetFileEncode(encode);
	return NS_OK;
}

/* void SetLocal (in string encode); */
NS_IMETHODIMP COnlyLyric::SetLocal(const char *encode)
{
	CCharsetConvert::GetIns().SetLocal(encode);
	return NS_OK;
}

NS_IMETHODIMP COnlyLyric::Observe(nsISupports *aSubject, const char *aTopic, const PRUnichar *aData)
{
	if (std::string(aTopic) == "quit-application")
	{
		CPlayerManager::GetIns().ReleasePlayer();
	}
	return NS_OK;
}
